package net.mingsoft.basic.util;

import org.springframework.web.client.*;
import javax.servlet.http.*;
import java.util.*;
import org.springframework.util.*;
import org.springframework.http.*;

public class RestTemplateUtil
{
    private static RestTemplate restTemplate;
    
    public static HttpHeaders getHeaders(final HttpServletRequest request) {
        final HttpHeaders requestHeaders = new HttpHeaders();
        final Enumeration<String> headerNames = (Enumeration<String>)request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            final String key = headerNames.nextElement();
            final String value = request.getHeader(key);
            requestHeaders.add(key, value);
        }
        return requestHeaders;
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Map<String, String> headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return get(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final HttpHeaders headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<?> requestEntity = (HttpEntity<?>)new HttpEntity((MultiValueMap)headers);
        return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Map<String, String> headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return get(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final HttpHeaders headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<?> requestEntity = (HttpEntity<?>)new HttpEntity((MultiValueMap)headers);
        return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, (Object)HttpEntity.EMPTY, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return post(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return post(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return post(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return post(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.POST, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.POST, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final Class<T> responseType, final Object... uriVariables) {
        return put(url, (HttpEntity<?>)HttpEntity.EMPTY, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody);
        return put(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody);
        return put(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return put(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return put(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return put(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return put(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.PUT, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> put(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.PUT, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Class<T> responseType, final Object... uriVariables) {
        return delete(url, (HttpEntity<?>)HttpEntity.EMPTY, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return delete(url, (HttpEntity<?>)HttpEntity.EMPTY, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Map<String, String> headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return delete(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpHeaders headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity((MultiValueMap)headers);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Map<String, String> headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return delete(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpHeaders headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity((MultiValueMap)headers);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return delete(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return delete(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return delete(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.DELETE, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> delete(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.DELETE, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> exchange(final String url, final HttpMethod method, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, method, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> exchange(final String url, final HttpMethod method, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, method, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static RestTemplate getRestTemplate() {
        return RestTemplateUtil.restTemplate;
    }
    
    static {
        RestTemplateUtil.restTemplate = new RestTemplate();
    }
}
